package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.mapper.CheckgroupCheckitemMapper;
import com.ujiuye.mapper.CheckgroupMapper;
import com.ujiuye.pojo.Checkgroup;
import com.ujiuye.pojo.CheckgroupCheckitem;
import com.ujiuye.service.ICheckgroupService;
import com.ujiuye.utils.MessageConstant;
import com.ujiuye.utils.PageResult;
import com.ujiuye.utils.QueryPageBean;
import com.ujiuye.utils.Result;
import com.ujiuye.vo.CheckgroupCheckitemVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 检查组 服务实现类
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@Service
public class CheckgroupServiceImpl extends ServiceImpl<CheckgroupMapper, Checkgroup> implements ICheckgroupService {

    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    @Override
    public PageResult listPage(QueryPageBean queryPageBean) {
        //创建分页对象
        Page<Checkgroup> page =
                new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        //创建条件构造器
        QueryWrapper<Checkgroup> queryWrapper = new QueryWrapper<>();
        //判断查询条件是否为空
        if (queryString!=null && queryString.length()>0){
            queryWrapper.eq("code",queryString)
                    .or().eq("name",queryString)
                    .or().eq("helpCode",queryString);
        }
        //执行查询
        Page<Checkgroup> checkgroupPage = checkgroupMapper.selectPage(page, queryWrapper);
        //创建返回结果对象
        PageResult pageResult = new PageResult(checkgroupPage.getTotal(),checkgroupPage.getRecords());
        return pageResult;
    }

    @Override
    public void save(Integer[] checkitemIds, Checkgroup checkgroup) {
        //添加检查组，添加成功后主键会自动返回checkGroup的id属性
        checkgroupMapper.insert(checkgroup);
        //添加中间表
        for (Integer checkitemId : checkitemIds) {
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();
            checkgroupCheckitem.setCheckgroupId(checkgroup.getId());
            checkgroupCheckitem.setCheckitemId(checkitemId);
            checkgroupCheckitemMapper.insert(checkgroupCheckitem);
        }
    }

    @Override
    public Result getById(int id) {
        //查询检查组的信息
        Checkgroup checkgroup = checkgroupMapper.selectById(id);
        //查询这个检查组有什么检查项
        QueryWrapper<CheckgroupCheckitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("checkgroup_id",id);
        List<CheckgroupCheckitem> checkgroupCheckitems = checkgroupCheckitemMapper.selectList(queryWrapper);
        /*
        checkgroupCheckitems
        [{34,29},{34,30},{34,28}] ----->    [28,29,30]
         */
        List<Integer> checkitemIds = new ArrayList<>();
        for (CheckgroupCheckitem checkgroupCheckitem : checkgroupCheckitems) {
            checkitemIds.add(checkgroupCheckitem.getCheckitemId());
        }
        //把checkgroup和checkitemIds组装在一起
        CheckgroupCheckitemVo checkgroupCheckitemVo = new CheckgroupCheckitemVo(checkgroup,checkitemIds);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupCheckitemVo);
    }

    @Override
    public void update(Integer[] checkitemIds, Checkgroup checkgroup) {
        //更新检查组，添加成功后主键会自动返回checkGroup的id属性
        checkgroupMapper.updateById(checkgroup);
        //修改检查项，把原来的删除(中间表记录)再重新添加
        QueryWrapper<CheckgroupCheckitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("checkgroup_id",checkgroup.getId());
        checkgroupCheckitemMapper.delete(queryWrapper);
        //重新添加中间表
        for (Integer checkitemId : checkitemIds) {
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();
            checkgroupCheckitem.setCheckgroupId(checkgroup.getId());
            checkgroupCheckitem.setCheckitemId(checkitemId);
            checkgroupCheckitemMapper.insert(checkgroupCheckitem);
        }
    }

}
